﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows;
using System.ComponentModel;

namespace IVS.Calculator
{
    /// <summary>
    /// Utilities class can come in handy in various situations.
    /// More like class, it's set of methods for various usage.
    /// </summary>
    public static class Utilities
    {
        #region ------------------------------ PUBLIC METHODS ------------------------------

        /// <summary>
        /// Returns TRUE if object is binary operation, FALSE otherwise.
        /// </summary>
        /// <param name="thisObject">sender</param>
        /// <returns></returns>
        public static bool IsBinary (this Operations thisObject)
        {
            if (thisObject == Operations.Addition ||
                thisObject == Operations.Subtraction ||
                thisObject == Operations.Multiplication ||
                thisObject == Operations.Division ||
                thisObject == Operations.Power)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// Returns TRUE if object is unary operation, FALSE otherwise.
        /// </summary>
        /// <param name="thisObject">sender</param>
        /// <returns></returns>
        public static bool IsUnary (this Operations thisObject)
        {
            if (thisObject == Operations.SquareRoot ||
                thisObject == Operations.Factorial ||
                thisObject == Operations.Inversion ||
                thisObject == Operations.Negation)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// Converts string object to displayable format.
        /// </summary>
        /// <param name="thisObject"></param>
        /// <param name="value">numeric representation of string object</param>
        /// <returns></returns>
        public static string FormatToDisplay (this string thisObject, double value)
        {
            const double maxValue = 10e+13F;
            const int maxLength = 14;
            if (thisObject.Length <= maxLength)
                return thisObject;

            string result = null;
            if (value < maxValue)
            {
                if (thisObject.Contains("."))
                {
                    result = thisObject.Substring(0, maxLength);
                    if (result.ToCharArray()[maxLength - 1] == '.')
                        result = result.Substring(0, maxLength - 1);
                }
            }

            return result;
        }


        #endregion
    }
}
